#include <stdio.h>
#include <stdlib.h>

int* queue = NULL;
int  q_len = 0;
int* list = NULL;
int  l_len = 0;
int  listcur = 0;
int  rp = 1;

void init(int** q, int n)
{
    int i = 0;
    *q = (int*)malloc(n*sizeof(int));

    for(i = 0; i < n; i++)
    {
        (*q)[i] = 0;
    }
}

void freeq(int** q, int n)
{
    if(*q == NULL)
    {
        return;
    }
    free(*q);
    *q = NULL;
}

static short int rcount = 0;
void removefn()
{
    int i = 0;

    rcount++;
    if(rp == 1)
    {
        for(i = 0; queue[i] == 0 && i < q_len; i++)
        {
            ;
        }
    }
    else
    {
        for(i = q_len-1; queue[i] == 0 && i >= 0; i--)
        {
            ;
        }
    }
    if(rcount == list[listcur])
    {
        if(i >= q_len || i < 0)
        {
            printf("-1\r\n");
            return;
        }
        else
        {
            printf("%d\r\n", i+1);
        }
        listcur++;
    }
    queue[i] = 0;
}

void initp()
{
    rcount = 0;
    rp = 1;
    listcur = 0;
}

void process()
{
    int i = 0;
    char opstr[2];
    int opval;

    while(1)
    {
        initp();
        if(scanf("%d", &q_len) == EOF)
        {
            break;
        }
        init(&queue, q_len);
        scanf("%d", &l_len);
        init(&list, l_len);

        for(i = 0; i < l_len; i++)
        {
            scanf("%d", &list[i]);
        }

        while(1)
        {
            scanf("%s", opstr);
            if(opstr[0] == 'e')
            {
                break;
            }
            switch(opstr[0])
            {
            case 'a':
                scanf("%d", &opval);
                queue[opval-1]++;
                break;
            case 'r':
                removefn();
                break;
            case 'p':
                scanf("%d", &rp);
                break;
            default:
                exit(-1);
                break;
            }
        }
        freeq(&queue, q_len);
        freeq(&list, l_len);
        printf("\n");
    }

}

int main()
{
    //freopen("bin\\debug\\in.txt", "r", stdin);

    process();

    return 0;
}
